package com.teacher.pc.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.teacher.common.CommonResult;
import com.teacher.pojo.dto.login.LoginDto;
import com.teacher.pojo.dto.teacher.TeacherGetByDeptDto;
import com.teacher.pojo.dto.teacher.TeacherUpdateDto;
import com.teacher.pojo.entity.PerTeacher;
import com.teacher.service.IPerTeacherService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentMap;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-06-15
 */
@RestController
@RequestMapping("/pc/per-teacher")
public class PerTeacherController {

    @Autowired
    private IPerTeacherService iPerTeacherService;


    /**
     * 登录
     *
     * @param dto
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public CommonResult<ConcurrentMap<String,Object>> login(@RequestBody LoginDto dto) {
        return iPerTeacherService.login(dto);
    }

    /**
     * @return
     */
    @GetMapping("/menu")
    @ApiOperation("获取菜单")
    public CommonResult getMenu() {
        return iPerTeacherService.getMenu();
    }

    /**
     * logout
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("用户登出")
    public CommonResult logoutUser() {
        return iPerTeacherService.logout();
    }

    /**
     * 根据部门查询教师用户
     *
     * @param dto
     * @return
     */
    @GetMapping("/department")
    @ApiOperation("根据部门查询教师用户")
    public CommonResult getByDepartment(TeacherGetByDeptDto dto) {
        return iPerTeacherService.getByDepartmentId(dto);
    }

    /**
     * delete user by user's id
     *
     * @param ids list of user's id
     * @return delete whether success
     */
    @DeleteMapping
    @ApiOperation("删除教师用户")
    public CommonResult deleteUser(@RequestBody List<String> ids) {
        List<PerTeacher> perTeacherList = iPerTeacherService.list(new QueryWrapper<PerTeacher>().lambda()
                .in(PerTeacher::getId, ids));
        List<Long> weUserIds = new ArrayList<>();
        for (PerTeacher perTeacher : perTeacherList) {
            weUserIds.add(perTeacher.getWeUserId());
        }

        if (iPerTeacherService.remove(new QueryWrapper<PerTeacher>().lambda()
                .in(PerTeacher::getId, ids))) {

            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }

    /**
     * 更新信息
     *
     * @param dto
     * @return
     */
    @PutMapping("/user")
    @ApiOperation("更新信息")
    public CommonResult updateUser(@RequestBody TeacherUpdateDto dto) {
        return iPerTeacherService.updateInfo(dto);
    }

    /**
     * 远程搜索班级
     *
     * @param name
     * @return
     */
    @GetMapping("/byName")
    @ApiOperation("远程搜索老师")
    public CommonResult<List<PerTeacher>> getByName(String name) {
        List<PerTeacher> perTeacherList = iPerTeacherService.list(new QueryWrapper<PerTeacher>().lambda()
                .like(StrUtil.isNotEmpty(name), PerTeacher::getName, name));
        for (PerTeacher perTeacher : perTeacherList) {
            perTeacher.setName(perTeacher.getName() + "-" + perTeacher.getTeacherKey());
        }
        return CommonResult.success(perTeacherList);
    }
}

